<?php
$params = array_merge(
    require __DIR__ . '/../../common/config/params.php',
    require __DIR__ . '/../../common/config/params-local.php',
    require __DIR__ . '/params.php',
    require __DIR__ . '/params-local.php'
);

return [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'api\controllers',
    'language' => 'zh-CN',
    'timeZone' => 'Asia/Shanghai',
    'modules' => [
        'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',
            'tokenParamName' => 'accessToken',
            'tokenAccessLifetime' => 3600 * 24 * 7,
            'storageMap' => [
                'user_credentials'      => 'api\models\User',
                'access_token'          => [
                    'class' => 'api\modules\v1\oauth2\Pdo',
                ],
                'authorization_code'    => 'api\modules\v1\oauth2\Pdo',
                'client_credentials'    => 'api\modules\v1\oauth2\Pdo',
                'client'                => 'api\modules\v1\oauth2\Pdo',
                'refresh_token'         => [
                    'class' => 'api\modules\v1\oauth2\Pdo',
                    'expires' => 3600 * 24 * 60
                ],
                'public_key'            => 'api\modules\v1\oauth2\Pdo',
                'jwt_bearer'            => 'api\modules\v1\oauth2\Pdo',
                'scope'                 => 'api\modules\v1\oauth2\Pdo',
            ],
            'grantTypes' => [
                'user_credentials' => [
                    'class' => 'OAuth2\GrantType\UserCredentials',
                ],
                'refresh_token' => [
                    'class' => 'OAuth2\GrantType\RefreshToken',
                    'always_issue_new_refresh_token' => true,
                ],
                'client_credentials' => [
                    'class' => 'OAuth2\GrantType\ClientCredentials',
                ],
                'authorization_code' => [
                    'class' => 'OAuth2\GrantType\AuthorizationCode'
                ],
            ],

            'components' => [
                'request' => function () {
                    return \filsh\yii2\oauth2server\Request::createFromGlobals();
                },
                'response' => [
                    'class' => \filsh\yii2\oauth2server\Response::class,
                ],
            ],
        ],
        'v1' => [
            'class' => 'api\modules\v1\Module',
        ],
    ],
    'components' => [
        'request' => [
            'class' => '\yii\web\Request',
            'enableCookieValidation' => false,  //禁用cookies
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ],
        ],
        'user' => [
            'identityClass' => 'api\models\User',
            'enableAutoLogin' => true,
            'enableSession' => false,
            'loginUrl' => null, #显示一个HTTP 403 错误而不是跳转到登录界面
            'idParam' => '_id-user'
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                    'logVars' => ['*'],
                    'logFile' => '@logs/app/'.date('Ymd').'.log',
                ],
                [
                    'class' => 'yii\log\FileTarget',
                    'categories' => ['user'],
                    'levels' => ['error', 'warning'],
                    'logVars' => ['*'],
                    'logFile' => '@logs/user/'.date('Ymd').'.log',
                    'maxFileSize' => 1024 * 2,
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                'POST v1/oauth2/<action:\w+>' => 'v1/oauth2/<action>',
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => ['v1/user'],
                    'pluralize' => false,
                    'extraPatterns' => [
                        'GET test' => 'test',
                        'POST signup' => 'signup',
                        'POST login' => 'login',
//                        'PATCH view' => 'view',
                    ]
                ],
            ],

        ],
        'i18n' => [
            'translations' => [
                'app*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
//                    'basePath' => '@api/messages',
//                    'sourceLanguage' => 'en-US',
                    'fileMap' => [
                        'app' => 'app.php',
                        'app/error' => 'error.php',
                    ],
                ],
            ],
        ],
    ],
    'params' => $params,
];
